﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace DataWorks_Tools.SpikeDetails
{
    public static class Filter
    {
        /// <summary>
        /// 移动平均,曲线平滑
        /// </summary>
        /// <param name="rawData">原曲线数组</param>
        /// <param name="step">步长</param>
        /// <returns></returns>
        public static double[] Smoothing(double[] rawData, int step = 3)
        {
            double[] smooth = new double[rawData.Length];
            unsafe
            {
                fixed (double* o = smooth, r = rawData)
                {
                    for (int i = step; i < rawData.Length; i++)
                    {
                        double total = 0;
                        int s = step * 2 + 1;
                        for (int j = i - step; j < i + step + 1; j++)
                        {
                            if (j < rawData.Length)
                            {
                                total += r[j];
                            }
                            else
                            {
                                break;
                            }
                        }
                        o[i] = total / s;
                    }

                    //Head fill
                    for (int i = 0; i < step; i++)
                    {
                        o[i] = o[step];
                    }
                    //Tail fill
                    int tail = rawData.Length - (rawData.Length % (step + 1)) - 1;
                    for (int j = tail; j < rawData.Length; j++)
                    {
                        o[j] = o[tail - 1];
                    }

                }
            }
          
            return smooth;
        }

        /// <summary>
        /// 中值滤波,去毛刺
        /// </summary>
        /// <param name="rawData"></param>
        /// <param name="step"></param>
        /// <returns></returns>
        public static double[] MedianFilter(double[] rawData, int step = 3)
        {
            int length = step * 2 + 1;
            double[] smooth = new double[rawData.Length];
            double[] median = new double[length];
            unsafe
            {
                fixed (double* o = smooth, r = rawData, m = median)
                {
                    for (int i = step; i < rawData.Length; i++)
                    {
                        int s = i - step;
                        int k = 0;
                        for (int j = i - step; j < i + step + 1; j++)
                        {
                            if (j < rawData.Length)
                            {
                                m[k] = r[j];
                            }
                            else
                            {
                                break;
                            }
                            k++;
                        }
                        o[i] = BubbleSort.SortBubbleAscendingOrder(median)[step];//排序取中间值,在我的上一篇博客有源码
                    }
                    //Head fill
                    for (int i = 0; i < step; i++)
                    {
                        o[i] = o[step];
                    }
                    //Tail fill
                    int tail = rawData.Length - (rawData.Length % (step + 1)) - 1;
                    for (int j = tail; j < rawData.Length; j++)
                    {
                        o[j] = o[tail - 1];
                    }

                }
            }
           
            return smooth;
        }


        /// RC低通滤波
        /// </summary>
        /// <param name="DataArray">数据源</param>
        /// <param name="fc">截止频率</param>
        /// <param name="fl">采样频率</param>
        public static double[] RCLowPass(double[] DataArray, double fc, double fl)
        {
            double a = fc * 2 * Math.PI / fl; //滤波系数
            double[] result = new double[DataArray.Length];
            result[0] = DataArray[0];
            for (int i = 1; i < DataArray.Length; i++)
            {
                result[i] = a * DataArray[i] + (1 - a) * result[i - 1];
            }
            return result;
        }

        public static double[] RatioPass(double[] DataArray,double mean,double ratio)
        {
            double[] result = new double[DataArray.Length];
            
            for (int i = 0; i < DataArray.Length; i++)
            {
                result[i] = (DataArray[i]-mean)* ratio + mean;
            }
            return result;
        }


        public enum peakorvalley
        {
            peak=-2,
            valley=2

        }
        /// <summary>
        /// 提取峰谷值
        /// </summary>
        /// <param name="lst">数据源</param>
        /// <returns>List<double></returns>
        public static List<double> Findpeaks(this List<double> lst, int peakorvalley)
        {
            List<double> repv = new List<double>();
            if (lst.Count > 0)
            {
                List<int> diff = new List<int>();
                List<int> diff2 = new List<int>();
                List<int> zeroindex = new List<int>();
                List<int> pvindex = new List<int>();
                //差分计算后一个点-前一个点
                for (int i = 0; i < lst.Count - 1; i++)
                {
                    if (lst[i + 1] - lst[i] > 0)
                    {
                        diff.Add(1);
                    }
                    else if (lst[i + 1] - lst[i] == 0)
                    {

                        diff.Add(0);
                    }
                    else
                    {
                        diff.Add(-1);
                    }
                }
                //如果是0点再看前一个点是否不为0来判断,用2个list来保存需要改为1和-1的index
                List<int> positivelist = new List<int>();
                List<int> nagitivelist = new List<int>();
                for (int i = 1; i < diff.Count; i++)
                {
                    if (diff[i] == 0 && diff[i - 1] < 0)
                    {
                        nagitivelist.Add(i);
                    }
                    else if (diff[i] == 0 && diff[i - 1] > 0)
                    {
                        positivelist.Add(i);
                    }
                }
                //改1和-1
                for (int i = 0; i < positivelist.Count; i++)
                {
                    diff[positivelist[i]] = 1;
                }
                for (int i = 0; i < nagitivelist.Count; i++)
                {
                    diff[nagitivelist[i]] = -1;
                }
                //再进行差分，最后只有-2,0,2。-2为峰，2为谷
                for (int i = 0; i < diff.Count - 1; i++)
                {
                    diff2.Add(diff[i + 1] - diff[i]);
                }
                for (int i = 0; i < diff2.Count; i++)
                {
                    if (diff2[i] == peakorvalley)
                    {
                        pvindex.Add(i + 1);
                    }
                }

                for (int i = 0; i < pvindex.Count; i++)
                {
                    repv.Add(lst[pvindex[i]]);
                }
            }
            return repv;
        }

        /// <summary>
        /// 线性归一化处理
        /// </summary>
        /// <param name="inputList"></param>
        /// <returns></returns>
        public static List<double> LinearNormalize(List<double> inputList)
        {
            List<double> normalizedList = new List<double>();

            // 找到列表中的最大值和最小值
            double min = inputList.Min();
            double max = inputList.Max();

            // 归一化处理
            foreach (double value in inputList)
            {
                double normalizedValue = (value - min) / (max - min);
                normalizedList.Add(normalizedValue);
            }

            return normalizedList;
        }


        /// <summary>
        /// 标准化处理
        /// </summary>
        /// <param name="inputList"></param>
        /// <returns></returns>
        public static List<double> Standardize(List<double> inputList)
        {
            List<double> standardizedList = new List<double>();

            // 计算平均值
            double mean = inputList.Average();

            // 计算标准差
            double sumOfSquares = 0.0;
            foreach (double value in inputList)
            {
                sumOfSquares += Math.Pow(value - mean, 2);
            }
            double standardDeviation = Math.Sqrt(sumOfSquares / inputList.Count);

            // 标准化处理
            foreach (double value in inputList)
            {
                double standardizedValue = (value - mean) / standardDeviation;
                standardizedList.Add(standardizedValue);
            }

            return standardizedList;
        }

        /// <summary>
        /// 计算峰谷值的平均range
        /// </summary>
        /// <param name="lst"></param>
        /// <returns></returns>
        public static double CalRangeAvg(this List<double> lst,int points)
        {
            List<double> differences = new List<double>();
            for (int i = 0; i < lst.Count; i += points)
            {
                if (i + points <= lst.Count)
                {
                    double max = lst.GetRange(i, points).Max();
                    double min = lst.GetRange(i, points).Min();
                    differences.Add(Math.Abs(max - min));
                }
            }
            return differences.Average();
        }

    }
}
